package com.whw.springboot.bcucb.service.serviceImpl;

import com.whw.springboot.bcucb.entity.Message;
import com.whw.springboot.bcucb.entity.Userinfo;
import com.whw.springboot.bcucb.mapper.UserinfoMapper;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.whw.springboot.bcucb.mapper.UserMapper;
import com.whw.springboot.bcucb.entity.User;
import com.whw.springboot.bcucb.service.UserService;
@Service
public class UserServiceImpl implements UserService{

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserinfoMapper userinfoMapper;

    @Override
    public Message<?> deleteByPrimaryKey(String userId) {
        return userMapper.deleteByPrimaryKey(userId)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message<?> insert(User record) {
        return userMapper.insert(record)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message<?> insertSelective(User record) {
        return userMapper.insertSelective(record)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message<User> selectByPrimaryKey(String userId) {
        return  Message.success(null).add(userMapper.selectByPrimaryKey(userId));
    }


    @Override
    public Message<?> updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message<?> updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message<?> login(String user_id,String user_pwd){
        if(userinfoMapper.selectByPrimaryKey(user_id)!=null){
            Userinfo userinfo = userinfoMapper.selectByPrimaryKey(user_id);
            if(userMapper.selectByPrimaryKey(user_id)!=null){
                User user = userMapper.selectByPrimaryKey(user_id);
                if(user.getUserPwd().equals(user_pwd)){
                    return Message.success(null).add(userMapper.selectByPrimaryKey(user_id));
                }else{
                    return Message.fail("用户密码错误");
                }
            }else{
                if(user_pwd.equals("123456")){
                    String user_level = "1";
                    if(userinfo.getUserinfoType()==0){
                        user_level="1";
                    }else if(userinfo.getUserinfoType()==2){
                        user_level="3";
                    }else if(userinfo.getUserinfoType()==1){
                        user_level="4";
                    }else{
                        user_level="1";
                    }
                    User user = new User();
                    user.setUserId(userinfo.getUserinfoId());
                    user.setUserPwd("123456");
                    user.setUserLevel(user_level);
                    userMapper.insert(user);
                    return Message.success(null).add(userMapper.selectByPrimaryKey(user_id));
                }else{
                    return Message.fail("用户密码错误");
                }
            }
        }else{
            return Message.fail("用户没有资格");
        }
    }

    @Override
    public Message<?> adminLogin(String user_id,String user_pwd){
        if(userMapper.selectByPrimaryKey(user_id)!=null){
            User user = userMapper.selectByPrimaryKey(user_id);
            if(user.getUserLevel().equals("4")){
                if(user.getUserPwd().equals(user_pwd)){
                    return Message.success(null).add(user);
                }else{
                    return Message.fail("密码错误");
                }
            }else{
                return Message.fail("此用户不是管理员");
            }
        }else{
            return Message.fail("用户没有资格");
        }

    }




}
